﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;

//using Yeex.Common;
using HanhuaMicroCredit.BLL.File;
using Library.Common;

namespace Yeex.Boss.Web.Controllers
{
    public class FileController : Controller
    {
        public ActionResult Upload(string id, string u, string o, string type)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = "default";
            }
            Dictionary<string, object> data = UploadBLL.GetUploadConfigs(id);
            ViewData["ExtendNames"] = string.Format("'{0}'", data["filetypes"].ToString().Replace(",", "','"));
            ViewData["FileUrlInput"] = u;
            ViewData["OriginalPathInput"] = o;

            if (string.IsNullOrEmpty(type) || type == "html")
            {
                return View(data);
            }
            else
            {
                return View("UploadSWF", data);
            }
        }

        [HttpPost]
        public ActionResult Upload(string id, FormCollection values)
        {
            List<HttpPostedFileBase> files = new List<HttpPostedFileBase>();
            int fileCount = Request.Files.Count;
            for (int i = 0; i < fileCount; i++)
            {
                HttpPostedFileBase f = Request.Files[i];
                if (f.ContentLength == 0)
                {
                    continue;
                }
                files.Add(f);
            }
            string fileUrls = string.Empty;
            string errorMsg = UploadBLL.Upload(id, files, out fileUrls);

            StringBuilder html = new StringBuilder();
            html.Append("<script type=\"text/javascript\">");
            html.AppendFormat("parent.uploadFinish('{0}', '{1}', '{2}');", errorMsg.Replace('\'', '"').Replace('\n', ' ').Replace('\r', ' '), values["FileUrlControlName"], fileUrls);
            html.Append("</script>");

            return Content(html.ToString());
        }

        public FileResult Download(int id)
        {
            return File("", "", "");
        }

        public ActionResult AjaxUpload(string id, string cb)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = "default";
            }
            Dictionary<string, object> data = UploadBLL.GetUploadConfigs(id);
            if (!data.ContainsKey("Callback"))
            {
                data.Add("Callback", cb);
            }

            return View(data);
        }

        [HttpPost]
        public JsonResult AjaxUpload(string id, FormCollection values)
        {
            List<HttpPostedFileBase> files = new List<HttpPostedFileBase>();
            int fileCount = Request.Files.Count;
            for (int i = 0; i < fileCount; i++)
            {
                HttpPostedFileBase f = Request.Files[i];
                if (f.ContentLength == 0)
                {
                    continue;
                }
                files.Add(f);
            }
            string fileUrls = string.Empty;
            string errorMsg = UploadBLL.Upload(id, files, out fileUrls);

            Dictionary<string, string> result = new Dictionary<string, string>();
            result.Add("msg", errorMsg);
            result.Add("url", fileUrls);

            return Json(result, "text/plain");
        }

        public ActionResult Config()
        {
            return View();
        }

        public ActionResult List(string valid)
        {
            return View(FileBLL.GetList(valid));
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ContentResult Add(FormCollection values)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            foreach (string key in values.Keys)
            {
                data.Add(key, values[key]);
            }

            string errorInfo = FileBLL.AddOrUpdate(0, data);
            if (string.IsNullOrEmpty(errorInfo))
            {
                return Content(JUIResult.ResultMsg("1", "文件上传参数配置添加成功！"));
            }

            return Content(JUIResult.ResultMsg(errorInfo));
        }

        public ActionResult Edit(int id)
        {
            return View(FileBLL.GetModel(id));
        }

        [HttpPost]
        public ContentResult Edit(int id, FormCollection values)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            foreach (string key in values.Keys)
            {
                data.Add(key, values[key]);
            }

            string errorInfo = FileBLL.AddOrUpdate(id, data);
            if (string.IsNullOrEmpty(errorInfo))
            {
                return Content(JUIResult.ResultMsg("1", "文件上传参数配置修改成功！"));
            }

            return Content(JUIResult.ResultMsg(errorInfo));
        }
    }
}
